package com.sxt.bus.utils;

import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;

import com.sxt.bus.domain.Customer;

/**
 * 导出客户数据
 * 
 * @author LJH
 * 
 */
public class ExportCustomerUtils {

	/**
	 * 导出客户数据
	 * 
	 * @param customers
	 * @param fileName
	 */
	@SuppressWarnings("deprecation")
	public static void export(List<Customer> customers, String fileName,HttpServletResponse response) {
		// 创建工作簿
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 创建sheet
		HSSFSheet sheet = workbook.createSheet("客户列表");
		sheet.setDefaultColumnWidth(25);
		// 合并
		CellRangeAddress region1 = new CellRangeAddress(0, 0, 0, 5);
		CellRangeAddress region2 = new CellRangeAddress(1, 1, 0, 5);
		sheet.addMergedRegion(region1);
		sheet.addMergedRegion(region2);
		// 得到样式
		HSSFCellStyle titleStyle = HSSFStyleUtils.getTitleStyle(workbook);
		HSSFCellStyle secoundTitleStyle = HSSFStyleUtils
				.getSecoundTitleStyle(workbook);
		HSSFCellStyle tableTitleStyle = HSSFStyleUtils
				.getTableTitleStyle(workbook);
		HSSFCellStyle tableBodyStyle = HSSFStyleUtils
				.getTableBodyStyle(workbook);

		// 创建第一行
		int row = 0;
		HSSFRow row1 = sheet.createRow(row);
		HSSFCell row1_cell1 = row1.createCell(0);
		row1_cell1.setCellStyle(titleStyle);
		row1_cell1.setCellValue("客户列表");

		// 第二行
		row++;
		HSSFRow row2 = sheet.createRow(row);
		HSSFCell row2_cell1 = row2.createCell(0);
		row2_cell1.setCellStyle(secoundTitleStyle);
		row2_cell1.setCellValue("总条数:" + customers.size() + "  导出时间:"
				+ new Date().toLocaleString());

		// 第三行
		row++;
		String[] titles = { "身份证号", "客户姓名", "客户地址", "客户性别", "客户电话", "客户职位" };
		HSSFRow row3 = sheet.createRow(row);
		for (int i = 0; i < titles.length; i++) {
			HSSFCell cell = row3.createCell(i);
			cell.setCellStyle(tableTitleStyle);
			cell.setCellValue(titles[i]);
		}
		// 第四行到最后
		for (int i = 0; i < customers.size(); i++) {
			row++;
			Customer customer = customers.get(i);
			// 创建行
			HSSFRow r = sheet.createRow(row);
			HSSFCell r_c1 = r.createCell(0);
			r_c1.setCellStyle(tableBodyStyle);
			r_c1.setCellValue(customer.getIdentity());

			HSSFCell r_c2 = r.createCell(1);
			r_c2.setCellStyle(tableBodyStyle);
			r_c2.setCellValue(customer.getCustname());

			HSSFCell r_c3 = r.createCell(2);
			r_c3.setCellStyle(tableBodyStyle);
			r_c3.setCellValue(customer.getAddress());

			HSSFCell r_c4 = r.createCell(3);
			r_c4.setCellStyle(tableBodyStyle);
			r_c4.setCellValue(customer.getSex() == 1 ? "男" : "女");

			HSSFCell r_c5 = r.createCell(4);
			r_c5.setCellStyle(tableBodyStyle);
			r_c5.setCellValue(customer.getPhone());

			HSSFCell r_c6 = r.createCell(5);
			r_c6.setCellStyle(tableBodyStyle);
			r_c6.setCellValue(customer.getCareer());
		}
		// 处理文件名
		try {
			fileName = URLEncoder.encode(fileName, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		response.reset();
		// 处理文件类型
		response.setContentType("application/x-download");
		// 设置下载的文件名
		response.setHeader("Content-Disposition", "attachment; filename=\""
				+ fileName + "\"");
		try {
			ServletOutputStream outputStream = response.getOutputStream();
			workbook.write(outputStream);
			workbook.close();
			outputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
